
#include "MCU_include.h"

void 	Error_Processing(void);
void	Var_Init(void);
void 	PowerOFF(void);
//void 	PotMoveInit(void);
void 	Debug_Code();


vu8		Pwm_Freq_Add_Dec_Check;
vu16		PhaseNum;
vu8		PhaseAngleStable = 95, PhaseAngle[3] = {95,95,95};	//Store phase width and phase angle

vu8		Time_1msCnt = 0, Time_1sCnt = 0,Time_10msCnt = 0;
vu16  	i;
vu16	u16_PowerSet, u16_PowerSetBack0, u16_PowerSetBack1;
vu16	u16_PowerSet_Target_H = 0, u16_PowerSet_Target_L = 0;
//vu32	vu32_PowerSet_H = 0, vu32_PowerSet_L = 0;
vu8		PotStatus = 0;
vu8		u8_MaxPhase = 0, PotMoveOutStatus = 1,PotMoveOutCnt = 0;
vu32    PWM_SF;
vu16    ADC_V_result,ADC_I_result;
vu16	PWM_F, PWM_F_Back, Lim_Frequ ;
vu8		OVP8_Cnt = 0, check_pot_num=0;
vu8 	PhaseDebounceCnt = 0;
vu8		PtmCnt = 0;
vu8		OVP1_Cnt = 0,	OVP2_TimeCnt = 0,OVP2_Cnt = 0,OVP2_ClrFlag = 0;
//vu8		BuzDelay = 0, FanDelay = 0;
vu8		I_PanStatus = 0;
vu16	I_AcChangeTH = 0;
vu8		DebugCnt,ErrRecoveryTimer;
u16		u16_TargetIh,u16_TargetIl;
vu16    errorCODE_u16,protectCODE_u16,test_u16;
volatile bit	fist_start_flag,	Pwm_Freq_HF_Flag , HF_Approach_ON ,HF_Approach_flag ,OVP8_ZERO,bSurge_error;


void main()
{
	System_Init();
	Var_Init();			
	bit_BUZZER = ON;
	bit_FAN = OFF;
	BuzDelay = 30;
	FanDelay = 5;	
	BUZZER_ON_300MS();
	FAN = OFF;
	fist_start_flag=0;
	
	while(1)			//system loop
	{
		_clrwdt();
		Time_Base();	//
		if(_tb0f == 1)
		{
			_tb0f = 0;
			_ctm2af = 0;
			CTM2_ON();	
			GCC_NOP();
		}
			
		if (_ptmaf)
		{
			_ptmaf = 0;
			PhaseDebounceCnt = 0;
			_pton = 0;
		}
		
		if (OVP2_ClrFlag)
		{
			OVP2_ClrFlag = 0;
			OVP2_Cnt = 0;
		}
		if(Time_Flag.Fg_1s == 1)
		{
				Time_Flag.Fg_1s =0 ;	
		//**************************************************//
		//**************************************************//		
				Debug_Code();
		//**************************************************//	
				//Check the cookware once every 1 second after it is removed during heating//
				Check_Pan_Movein_Phase();		//
					PhaseDebounceCnt = 0;
		//**************************************************//
		}
		if(Time_Flag.Fg_100ms == 1)
		{
			Time_Flag.Fg_100ms = 0;
			
			Error_Processing();					

			Surge_Protect();

		}
		if(	CheckPanTiming == 1)
		{
			CheckPanTiming = 0;
			Check_Pan_Moveout_Current();    //Current transfer pot detection	
			if(u16_PowerSet >1000)	
			Check_Pan_Moveout_Phase();		//Phase shift pot detection
		}
		if(Time_Flag.Fg_10ms == 1)
		{
			Time_Flag.Fg_10ms = 0;	//
	
									
			IIC_Check(200);			//
			BuzzerFan(360);			//
		}
		
		Get_PwoerSet_from_IIC() ; //Receive set power				
		Get_Power_Temp();		//		

		if ((ADC_FLAG.flagBit.Get_Power_Fg) && (ControlSet_App.flagBit.on_off == 1))//		//whf¶q§¹¥\²v¨Ã¥B³B©ó¶}¾÷ª¬ºA¤~¶i¤J
		{
			ADC_FLAG.flagBit.Get_Power_Fg = 0;
																
			PowerControl();			//power control 
									
		}
			
	
	}   
}                                



/************Variable initialization******************/
void Var_Init()
{
//	ControlSet_App.m_data  = 0;
	ADC_FLAG.m_data = 0;
	FLAG.m_data = 0;
	IH_Status_APP.m_data = 0;
	POWER_CTRL.m_data = 0;
	FG_IH_Status.flagBit.Machine_ON_OFF = 1;
//	IH_Status_APP.flagBit.NoPan = 1;
	FLAG.flagBit.PanMovOut  = 1;
	Debug_Mode.flagBit.LED_Fg = LED_DISPLAY_CTRL;
	I_PanStatus =1 ;
	DebugCnt = 0;
	u16_PowerSet=0;
	Debug_Mode.flagBit.DebFlag = 0;		
	
	_mss0 = 0;
	_mss1 = 0;
}



void Debug_Code()
{
#ifdef UART_ASCII

// ******************* uart debug*********************//
//****************************************************//

			uart_print_uint(ADC_I_result);
			uart_put_char('A');	
			uart_print_uint(PWM_F);
			uart_put_char('F');
			uart_print_uint(ADC_V_result);
			uart_put_char('V');
			uart_print_uint(R_PowerIV);
			uart_put_char('W');
			uart_print_uint(AD_Temp_IGBT);
			uart_put_char('T');	
			uart_print_uint(errorCODE_u16);
			uart_put_char('E');	
			uart_print_uint(protectCODE_u16);
			uart_put_char('R');
			
			uart_put_char(0x0d);			
			uart_put_char(0x0A);
			errorCODE_u16=0;
#endif
}

/********************* End of file **********************/